package com.yunhai.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.core.page.TableDataInfo;
import com.yunhai.system.domain.CompanyStatement;
import com.yunhai.system.domain.Environmental;
import com.yunhai.system.domain.News;
import com.yunhai.system.service.ICompanyStatementService;
import com.yunhai.system.service.IEnvironmentalService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 社会责任
 */
@RestController
@RequestMapping("/api/social/")
public class CompanyStatementController extends BaseController {
    @Autowired
    private ICompanyStatementService companyStatementService;
    @Autowired
    private IEnvironmentalService environmentalService;

    /**
     * 公司声明
     *
     * @return
     */
    @PostMapping("companyState")
    public TableDataInfo companyState(@RequestBody Map<String,Object> param) {
        Long page = MapUtils.getLong(param, "page", 1L);
        Long limit = MapUtils.getLong(param, "limit", 10L);
        QueryWrapper<CompanyStatement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<CompanyStatement> pageResult = companyStatementService.page(new Page<>(page, limit), queryWrapper);
        return new TableDataInfo(pageResult.getRecords(), (int) pageResult.getTotal());
    }

    /**
     * 环境公示
     * @param param
     * @return
     */
    @PostMapping("environmental")
    private TableDataInfo environmental(@RequestBody Map<String, Object> param) {
        Long page = MapUtils.getLong(param, "page", 1L);
        Long limit = MapUtils.getLong(param, "limit", 10L);
        QueryWrapper<Environmental> newsQueryWrapper = new QueryWrapper<>();
        newsQueryWrapper.select("id", "title");
        newsQueryWrapper.orderByDesc("create_time");
        IPage<Environmental> pageResult = environmentalService.page(new Page<>(page, limit), newsQueryWrapper);
        return new TableDataInfo(pageResult.getRecords(), (int) pageResult.getTotal());
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info")
    private AjaxResult info(Long id) {
        if (null == id) {
            return AjaxResult.error("id不能为空");
        }

        Environmental environmental = environmentalService.getById(id);
        if (null == environmental) {
            return AjaxResult.error("内容不存在");

        }
        return AjaxResult.success(environmental);
    }





}
